package cn.com.thingo.hyintelligent.ui.activity;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.th.opensdk.openapi.RemoterInfo;
import com.th.opensdk.openapi.THOpenSDK;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import cn.com.thingo.hyintelligent.R;
import cn.com.thingo.hyintelligent.app.App;
import cn.com.thingo.hyintelligent.base.BaseActivity;
import cn.com.thingo.hyintelligent.base.contract.DoorAuthContract;
import cn.com.thingo.hyintelligent.model.bean.CommonBean;
import cn.com.thingo.hyintelligent.model.bean.DoorLockAuthBean;
import cn.com.thingo.hyintelligent.presenter.DoorAuthPresenter;
import cn.com.thingo.hyintelligent.ui.adapter.DoorLockAuthAdapter;
import cn.com.thingo.hyintelligent.ui.adapter.decoration.RecycleViewDivider;
import cn.com.thingo.hyintelligent.util.SPUtils;
import cn.com.thingo.hyintelligent.util.ToastUtil;
import cn.com.thingo.hyintelligent.widget.AddAuthDialogFragment;
import cn.com.thingo.hyintelligent.widget.DeleteAuthDialogFragment;

/**
 * 门锁授权页面
 */
public class DoorAuthActivity extends BaseActivity<DoorAuthPresenter> implements DoorAuthContract.View, AddAuthDialogFragment.CallBack, DeleteAuthDialogFragment.CallBack, DoorLockAuthAdapter.OnAuthDeleteListener {

    @BindView(R.id.toolbar_left)
    ImageView ivBack;
    @BindView(R.id.toolbar_title)
    TextView tvTitle;
    @BindView(R.id.toolbar_right)
    ImageView ivAdd;
    @BindView(R.id.door_lock_auth_list)
    RecyclerView rvList;
    private DoorLockAuthAdapter adapter;
    private List<DoorLockAuthBean.DataBean> list = new ArrayList<>();

    @Override
    protected void initEventAndData() {
        initView();
    }

    @Override
    protected void onResume() {
        super.onResume();
        initData();
    }

    private void initData() {
        mPresenter.doorLockAuthList(SPUtils.newInstance().getString(SPUtils.LOGIN_TOKEN), App.getDataBean().getId());
    }

    private void initView() {
        ivBack.setImageResource(R.mipmap.back);
        tvTitle.setText("授权列表");
        ivAdd.setImageResource(R.mipmap.add);
        ivAdd.setVisibility(View.VISIBLE);
        rvList.setLayoutManager(new LinearLayoutManager(mContext));
        rvList.addItemDecoration(new RecycleViewDivider(mContext, LinearLayoutManager.VERTICAL, 2, getResources().getColor(R.color.gray_12)));
        adapter = new DoorLockAuthAdapter(mContext, list);
        adapter.setListener(this);
        rvList.setAdapter(adapter);
    }

    @OnClick({R.id.toolbar_left, R.id.toolbar_right})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.toolbar_left:
                finish();
                break;
            case R.id.toolbar_right:
                if (TextUtils.equals(SPUtils.newInstance().getString(SPUtils.GATEWAY_IS_CREATE), "1")) {
                    //是自己的网关
                    List<RemoterInfo> remoterList = THOpenSDK.getInstance().getRemoterList();
                    List<RemoterInfo> doorLockList = new ArrayList<>();
                    for (int i = 0; i < remoterList.size(); i++) {
                        if (remoterList.get(i).getRemoterType() == RemoterInfo.HOME_REMOTER_TYPE_LOCKER) {
                            doorLockList.add(remoterList.get(i));
                        }
                    }
                    //判断当前网关是否有门锁 todo 先屏蔽没门锁的授权弹窗
                    if (doorLockList.size() > 0) {
                    AddAuthDialogFragment fragment = new AddAuthDialogFragment(mContext, App.getDataBean().getId());
                    fragment.show(getFragmentManager(), "add_auth");
                    } else {
                        ToastUtil.shortShow("当前网关无门锁可授权！");
                    }
                } else {
                    //不是自己的网关
                    ToastUtil.shortShow("只能授权自己的网关！");
                }
                break;
        }
    }

    //添加授权的弹窗回调
    @Override
    public void determine(String telephone, String gatewayId) {
        mPresenter.addDoorAuth(SPUtils.newInstance().getString(SPUtils.LOGIN_TOKEN), gatewayId, telephone);
    }

    //删除授权的弹窗页面回调
    @Override
    public void determine(int position) {
        DoorLockAuthBean.DataBean dataBean = list.get(position);
        mPresenter.delDoorAuth(SPUtils.newInstance().getString(SPUtils.LOGIN_TOKEN), App.getDataBean().getId(), dataBean.getTelephone());

    }

    @Override
    protected int getLayout() {
        return R.layout.activity_door_auth;
    }

    @Override
    protected void initInject() {
        getActivityComponent().inject(this);

    }

    @Override
    public void viewAddDoorAuth(CommonBean commonBean) {
        ToastUtil.shortShow("授权成功！");
        initData();

    }

    @Override
    public void viewDelDoorAuth(CommonBean commonBean) {
        ToastUtil.shortShow("删除授权成功！");
        initData();

    }

    @Override
    public void viewDoorLockAuthList(DoorLockAuthBean doorLockAuthBean) {
        List<DoorLockAuthBean.DataBean> data = doorLockAuthBean.getData();
        list.clear();
        if (data != null && data.size() > 0) {
            list.addAll(data);
        }
        adapter.notifyDataSetChanged();
    }

    //点击删除之后的回调
    @Override
    public void authDelete(int position) {
        DeleteAuthDialogFragment fragment = new DeleteAuthDialogFragment(mContext, position);
        fragment.show(getFragmentManager(), "del_auth");
    }
}
